package com.lungle.warmdiary.app.base;

import android.content.Context;

import com.lungle.warmdiary.app.api.exception.ApiErrorCode;
import com.lungle.warmdiary.app.api.exception.ApiErrorHelper;
import com.lungle.warmdiary.app.api.exception.ApiException;
import com.lungle.warmdiary.app.utils.TDevice;

import rx.Subscriber;

/**
 *@author sbbic
 *created at 2016/9/11 21:32
 */
public class BaseSubscriber<T> extends Subscriber<T> {
    private Context mContext;

    public BaseSubscriber() {
    }

    public BaseSubscriber(Context context) {
        mContext = context;
    }

    @Override
    public void onStart() {
        if (!TDevice.hasInternet()) {
            this.onError(new ApiException(ApiErrorCode.ERROR_NO_INTERNET, "network interrupt"));
            return;
        }

    }


    @Override
    public void onCompleted() {

    }

    @Override
    public void onError(Throwable e) {

        ApiErrorHelper.handleCommonError(mContext,e);
//        if (e instanceof HttpException) {
//            AppContext.showToastShort("服务暂不可用");
//        } else if (e instanceof IOException) {
//            LogUtils.printLogI("服务暂不可用");
//        } else if (e instanceof ApiException) {
//            ApiException exception = (ApiException) e;
//            if (exception.isTokenExpried()) {
//                if (mContext != null && mContext instanceof Activity) {
//                    AlertDialog.Builder exitDialog = DialogHelper.getMessageDialog(mContext, "您的帐号在其他设备登录，请重新登录", (dialog, which) -> {
//                        dialog.dismiss();
//                        Intent intent = new Intent(mContext, LoginActivity.class);
////                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
//                        intent.putExtra(LoginActivity.BUNDLE_KEY_REQUEST_CODE, LoginActivity.REQUEST_CODE_RELOGIN);
//                        AppContext.getInstance().cleanToken();
//                        AppContext.getInstance().cleanLoginInfo();
//                        mContext.startActivity(intent);
//                    });
//                    exitDialog.setCancelable(false);
//                    exitDialog.show();
//                }
//
//            } else if (exception.isInvlidClient()) {
//                AppContext.showToast("非法客户端");
//            } else if (exception.getErrorCode() == ApiErrorCode.ERROR_PARAM_CHECK || exception.getErrorCode() == ApiErrorCode.ERROR_REQUEST_PARAM) {
//                LogUtils.printLogI("请求参数错误或者参数格式有误");
//            } else {
//                AppContext.showToastShort(  e.getMessage());
////                LogUtils.printLogE(e.getMessage());
//            }
//        } else {
//            AppContext.showToast("出现问题");
//        }
    }

    @Override
    public void onNext(T t) {

    }
}
